package com.ruoyi.develop.payApplyBuyDetail.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import com.ruoyi.develop.payApply.domain.PayApply;
import com.ruoyi.develop.payApply.service.IPayApplyService;
import com.ruoyi.develop.payApplyBuy.domain.PayApplyBuy;
import com.ruoyi.develop.payApplyBuy.service.IPayApplyBuyService;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.payApplyBuyDetail.domain.PayApplyBuyDetail;
import com.ruoyi.develop.payApplyBuyDetail.service.IPayApplyBuyDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单付款申请明细Controller
 * @author qh
 * @date 2025-03-22
 */
@RestController
@RequestMapping("/payApplyBuyDetail/payApplyBuyDetail")
public class PayApplyBuyDetailController extends BaseController {
    @Autowired
    private IPayApplyBuyDetailService payApplyBuyDetailService;
    @Autowired
    private IPayApplyService payApplyService;
    @Autowired
    private ExportServiceImpl exportService;
    @Autowired
    private ILvBuyService buyService;

    /**
     * 查询采购单付款申请明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:query')")
    @GetMapping("/list")
    public TableDataInfo list(PayApplyBuyDetail entity) {
        startPage();
        List<PayApplyBuyDetail> list = payApplyBuyDetailService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 查询采购单付款申请明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:query')")
    @GetMapping("/listByPayRecord")
    public TableDataInfo listByPayRecord(PayApplyBuyDetail entity) {
        startPage();
        List<PayApplyBuyDetail> list = payApplyBuyDetailService.listByPayRecord(entity);
        return getDataTable(list);
    }

    /**
     * 导出采购单付款申请明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:export')")
    @Log(title = "采购单付款申请明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(PayApplyBuyDetail entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        List<PayApplyBuyDetail> list = payApplyBuyDetailService.selectList(entity);
        // list 按照 buyId 去重
        List<PayApplyBuyDetail> listBuyDistinct = list.stream().collect(Collectors.toMap(PayApplyBuyDetail::getBuyId, a -> a, (k1, k2) -> k1)).values().stream().collect(Collectors.toList());
        for (PayApplyBuyDetail payApplyBuyDetail:listBuyDistinct){
            String buyId  =payApplyBuyDetail.getBuyId();
            if(StringUtils.isNotBlank(buyId)){
                LvBuy lvBuy = buyService.selectById(buyId);
                if (lvBuy != null) {
                    if (BusinessConstants.STATUS_YES.equals(lvBuy.getDepositMerge())) {
                        PayApplyBuyDetail detail = new PayApplyBuyDetail();
                        detail.setGoodsName("运费");
                        GoodsSalebuyall buyDetail = new GoodsSalebuyall();
                        buyDetail.setGoodsBuyTotal(lvBuy.getShippingFee());
                        detail.setBuyDetail(buyDetail);
                        detail.setMoney(lvBuy.getShippingFee());
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("buyNos", lvBuy.getNos());
                        detail.setParams(map);
                        list.add(detail);
                    }
                }
            }
        }

        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "付款申请单导出明细.xlsx";
        }
        String templateName = "付款申请明细.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
    }

    /**
     * 获取采购单付款申请明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(payApplyBuyDetailService.selectById(id));
    }

    /**
     * 新增采购单付款申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:add')")
    @Log(title = "采购单付款申请明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PayApplyBuyDetail entity) {
        return toAjax(payApplyBuyDetailService.insert(entity));
    }

    /**
     * 修改采购单付款申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:edit')")
    @Log(title = "采购单付款申请明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PayApplyBuyDetail entity) {
        return toAjax(payApplyBuyDetailService.update(entity));
    }

    /**
     * 删除采购单付款申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:PayApplyBuyDetail:remove')")
    @Log(title = "采购单付款申请明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(payApplyBuyDetailService.deleteByIds(ids));
    }






}
